package com.ssh.cp.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.ssh.cp.entity.FileClass;
import com.ssh.cp.idao.IFileClassDao;






@Repository
public class FileClassDao implements IFileClassDao{
	 @Resource(name="sessionFactory")   
	    private SessionFactory sessionFactory;  
	@Override
	public List<?> listFileClass() {
		// TODO Auto-generated method stub
		Session session=sessionFactory.openSession();
		List<?> list=null;
		try {
			@SuppressWarnings("rawtypes")
			Query q=session.createQuery("from fileclass");
			list=q.list();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			session.close();
		}
		
		
		return list;
	}

	@Override
	public void addClass(FileClass newclass) {  
        Session session = sessionFactory.getCurrentSession();
        try {
            session.save(newclass);  
        }catch(RuntimeException re) {			
			throw re;
		}
    }
	
	
	
	
	@Override
	public void delClass(int classid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query<?> query=null;
		try {
 			query=session.createQuery("delete from fileclass where classid=?");
 			query.setParameter(0, classid);
 			
 			query.executeUpdate();
 			}catch(RuntimeException re) {			
 				throw re;
 			}
	}

	@Override
	public void renameClass(FileClass aclass) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		/*Query<?> query=null;
		try {
			query=session.createQuery("update classname=? from fileclass where classid=?");
			
			query.setParameter(0, aclass.getClassname());
			query.setParameter(1, aclass.getClassid());
			query.executeUpdate();
 			}catch(RuntimeException re) {			
 				throw re;
 			}
 		*/	
		try {
			session.update(aclass);
		}catch(RuntimeException re) {			
				throw re;
			}
	}
	
	
	
	
}
